package com.asiainfo.dacp.datastash.broker.listener;

import com.asiainfo.dacp.core.BeanFactory;
import com.asiainfo.dacp.datastash.broker.core.vo.StashUnitTask;
import com.asiainfo.dacp.datastash.broker.facade.UnitTaskFacade;
import com.asiainfo.dacp.datastash.broker.facade.service.UnitDispatcher;
import com.asiainfo.dacp.datastash.broker.tracelog.StashTraceLogVo;
import com.asiainfo.dacp.datastash.broker.tracelog.StashTraceLogger;
import org.springframework.scheduling.annotation.Async;

import java.util.List;

/**
 *
 * 监听子任务/分片的待分配队列（unit_task_dispatch_queue）
 *
 * 获取任务后直接调用dispatch分配，如果没有资源重新回队
 *
 * @author MeiKefu
 * @date 2017-12-23
 */
public class UnitTaskDispatchQueueListener extends AbstractQueueListener {

    private String clzName = this.getClass().getSimpleName();

    @Async
    protected void handle(){
        StashTraceLogger tracelog = BeanFactory.getBean(StashTraceLogger.class);
        UnitTaskFacade unitTaskFacade = BeanFactory.getBean(UnitTaskFacade.class);

        StashUnitTask stashUnitTask = unitTaskFacade.fetchDispatchTask();

        if(stashUnitTask!=null){
            tracelog.info(StashTraceLogVo.buildStashTraceLogVo(stashUnitTask,clzName,"taskDispatchQueue:fetch","任务分配Work"));
            UnitDispatcher unitDispatcher = BeanFactory.getBean(UnitDispatcher.class);
            unitDispatcher.dispatch(stashUnitTask);
        }else{
            tracelog.debug(new StashTraceLogVo(clzName,"taskDispatchQueue:fetch:fail","未获取等待分配Work的任务"));
        }
    }

}
